package com.scd.jmeter.rmi.client;

import com.scd.jmeter.rmi.common.TestPlan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.UUID;

public class ClientMain {
    private static final Logger log = LoggerFactory.getLogger(ClientMain.class);

    public static int DEFAULT_RMI_PORT = 1099;

    public static void main(String[] args) throws IOException {
        String filePath = "jmeter.properties";
        if (args.length > 0) {
            filePath = args[0];
        }
        log.info("加载配置文件路径 {}", filePath);
        Properties properties = new Properties();
        properties.load(new FileInputStream(filePath));
        DEFAULT_RMI_PORT =  (int) properties.getOrDefault("server.rmi.port", 1099);
        DistributedRunner distributedRunner = new DistributedRunner(properties);
        String remoteHostsString = (String) properties.getOrDefault("remote_hosts", //$NON-NLS-1$
                "127.0.0.1");//NOSONAR $NON-NLS-1$
        log.info("配置远程服务地址 {}", remoteHostsString);
        StringTokenizer st = new StringTokenizer(remoteHostsString.trim(), ",");//$NON-NLS-1$
        List<String> hosts = new ArrayList<>();
        while (st.hasMoreElements()) {
            hosts.add(((String) st.nextElement()).trim());
        }
        String id = UUID.randomUUID().toString();
        log.info("当前测试计划ID {}", id);
        distributedRunner.init(hosts, new TestPlan(id));
        distributedRunner.start(hosts);
        log.info("远程调用完成");
    }
}
